package com.zlx.module_base.base_util;

import android.text.TextUtils;
import android.util.Log;

import com.jess.arms.database.MMkvHelper;
import com.orhanobut.logger.Logger;

/**
 * copyright (C), 2022, 运达科技有限公司
 * fileName YdBase
 *
 * @author 王玺权
 * date 2022-01-19 16:31
 * description 初始化
 * history
 */
public enum  YdBase {
    /**
     * 单例
     */
    INSTANCE;
    /**
     * 服务器地址
     */
    private String baseUrl;
    /**
     * 登录成功后主页类加载名
     */
    private String mainUrl;
    /**
     * 登录API接口地址
     */
    private String loginUrl;
    /**
     * 开发商名字
     */
    private String companyName;
    /**
     * app名字
     */
    private String appName;
    /**
     * logo地址
     */
    private int logoIconPath;
    /**
     * 当前APP版本
     */
    private String appVersion;

    /**
     * 服务名字
     */
    private String serviceName;

    /**
     * websocket地址
     */
    private String websocketUrl;

    /**
     * websocket可见性
     */
    private boolean websocketVisible;

    /**
     * app更新地址
     */
    private String appUpdateUrl;

    /**
     * app强制更新
     */
    private boolean compulsiveUpdate;

    /**
     * 启用离线模式
     */
    private boolean offLineMode=false;

    /**
     * 当前工作模式
     */
    private boolean currentMode=true;
    public YdBase setWebsocketUrl(String websocketUrl) {
        this.websocketUrl = websocketUrl;
        return this;
    }

    public YdBase setCompanyName(String companyName) {
        this.companyName = companyName;
        return this;
    }

    public YdBase setAppName(String appName) {
        this.appName = appName;
        return this;
    }

    public YdBase setAppLogo(int logoIconPath) {
        this.logoIconPath = logoIconPath;
        return this;
    }

    public YdBase setServiceName(String serviceName){
        this.serviceName=serviceName;
        return this;
    }



    public YdBase setAppVersion(String appVersion) {
        this.appVersion = appVersion;
        return this;
    }

    public YdBase setWebSocketVisible(boolean b) {
        this.websocketVisible = b;
        return this;
    }

    public YdBase setCompulsiveUpdate(boolean b) {
        this.compulsiveUpdate = b;
        return this;
    }

    YdBase() {

    }


    public YdBase setBaseUrl(String baseUrl) {
        this.baseUrl = baseUrl;
        if(!TextUtils.isEmpty(MMkvHelper.getInstance().getBaseUrl())){
            this.baseUrl = MMkvHelper.getInstance().getBaseUrl();
        }

        return this;
    }



    public YdBase setMainUrl(String mainUrl) {
        this.mainUrl = mainUrl;
        return this;
    }



    public YdBase setLoginUrl(String loginUrl) {
        this.loginUrl = loginUrl;
        return this;
    }

    public YdBase setAppUpdateUrl(String appUpdateUrl) {
        this.appUpdateUrl = appUpdateUrl;
        return this;
    }

    public YdBase setOffLineMode(Boolean offLineMode) {
        this.offLineMode = offLineMode;
        return this;
    }

    public YdBase setCurrentMode(Boolean currentMode) {
        this.currentMode = currentMode;
        return this;
    }
    public void init(){
        //当baseUrl不存在时
        try {
            MMkvHelper.getInstance().saveAppLogo(logoIconPath);
            MMkvHelper.getInstance().saveCompanyName(companyName);
            MMkvHelper.getInstance().saveAppName(appName);
            MMkvHelper.getInstance().saveAppVersion(appVersion);
            MMkvHelper.getInstance().saveAppUpdateUrl(appUpdateUrl);
            MMkvHelper.getInstance().webSocketIsVisible(websocketVisible);
            MMkvHelper.getInstance().appIscompulsiveUpdate(compulsiveUpdate);

            if(TextUtils.isEmpty(MMkvHelper.getInstance().getBaseUrl())){
                if(TextUtils.isEmpty(baseUrl)){
                    throw new RuntimeException("请在Application中设置baseUrl!");
                }else if(TextUtils.isEmpty(mainUrl)){
                    throw new RuntimeException("请在Application中设置登录成功后跳转地址!");
                }else if(TextUtils.isEmpty(loginUrl)){
                    throw new RuntimeException("请在Application中设置登录请求地址!");
                } else if(TextUtils.isEmpty(serviceName)){
                    throw new RuntimeException("请在Application中设置服务地址!");
                }else if(websocketVisible&&TextUtils.isEmpty(websocketUrl)){
                    throw new RuntimeException("请在Application中设置websocket地址!");
                }else {
                    if(TextUtils.isEmpty(MMkvHelper.getInstance().getBaseUrl())){
                        MMkvHelper.getInstance().saveLoginUrl(loginUrl);
                        MMkvHelper.getInstance().saveBaseUrl(baseUrl);
                        MMkvHelper.getInstance().saveMainUrl(mainUrl);
                        MMkvHelper.getInstance().saveServiceName(serviceName);
                        MMkvHelper.getInstance().saveWebsocketUrl(websocketUrl);
                        MMkvHelper.getInstance().saveAppUpdateUrl(appUpdateUrl);
                        MMkvHelper.getInstance().appIscompulsiveUpdate(compulsiveUpdate);
                        MMkvHelper.getInstance().offLineMode(offLineMode);
                        MMkvHelper.getInstance().currentMode(currentMode);
                    }
                }
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
        }
    }
}
